package com.xysq.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.Topics;
import com.xysq.adapter.HomepageAdapter;
import com.xysq.lemon.R;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadMoreListView;

import java.util.List;

import butterknife.InjectView;

public class OtherTopicsActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.list_other_topics)
    com.xysq.widget.LoadMoreListView otherTopicsList;
    @InjectView(R.id.txt_connect_or_public)
    TextView connectOrPublicTxt;
    @InjectView(R.id.txt_null_data)
    TextView nullDataTxt;
    HomepageAdapter otherTopicsAdapter;
    String title;
    public static final String CONNECT_OR_PUBLIC = "connectOrPublic";
    public static final String HOME_PAGE = "1";
    public static final String CONNECT_EVERYTHING = "2";
    public static final String PUBLIC = "3";
    public static final String MARKET = "4";
    public static final String BROWSE = "1";
    public static final String LIKE = "2";
    public static final String TITLE = "title";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_topics);
        String connectOrPublic = getExtra();
        initView(connectOrPublic);
        loadOtherTopics(connectOrPublic);
    }

    private String getExtra() {
        Intent intent = getIntent();
        title = intent.getStringExtra(TITLE);
        return intent.getStringExtra(CONNECT_OR_PUBLIC);
    }

    private void loadOtherTopics(String connectOrPublic) {
        String state = HOME_PAGE;
        if (connectOrPublic.equals(CONNECT_EVERYTHING)) {
            state = CONNECT_EVERYTHING;
        }
        if (connectOrPublic.equals(PUBLIC)) {
            state = PUBLIC;
        }
        appAction.getTopics(currentPage, PAGE_SIZE, state, new CallbackListener<List<Topics>>() {
            @Override
            public void onSuccess(List<Topics> data) {
                Log.e("++++成功了吗", data.size() + "");
                if(data.size() == 0){
                    nullDataTxt.setVisibility(View.VISIBLE);
//                    otherTopicsAdapter.setItems(data);
                }else {
                    otherTopicsAdapter.setItems(data);
                    nullDataTxt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("++++失败了吗", message);
            }
        });
    }

    private void initView(String connectOrPublic) {
        backIbtn.setOnClickListener(this);
        otherTopicsList.setOnItemClickListener(this);
        otherTopicsAdapter = new HomepageAdapter(this, appAction);
        otherTopicsList.setAdapter(otherTopicsAdapter);
        otherTopicsList.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                new LoadDataTask().execute();
            }
        });
        connectOrPublicTxt.setText(title);
    }

    @Override
    public void onClick(View view) {
        if (view == backIbtn) {
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Topics topics = (Topics) otherTopicsAdapter.getItem(i);
        appAction.behavior(topics.getUuid(), UserInfoKeeper.readCustomerId(OtherTopicsActivity.this), BROWSE, new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                Log.e("+++浏览成功", "浏览真的成功了");
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("+++浏览成功了吗", "睁大你的钛合金眼");
            }
        });
        Intent intent = new Intent(OtherTopicsActivity.this, TicketsListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(TicketsListActivity.TOPICS, topics);
        bundle.putString(TicketsListActivity.INDEX,"topics");
        intent.putExtra(TicketsListActivity.BUNDLE_TOPICS, bundle);
        startActivity(intent);
    }

    private class LoadDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            if (isCancelled()) {
                return null;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            otherTopicsAdapter.notifyDataSetChanged();
            otherTopicsList.onLoadMoreComplete();
            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled() {
            otherTopicsList.onLoadMoreComplete();
        }
    }
}
